package cn.com.xy.sms.sdk.ui.web;

import java.util.Locale;
import java.util.Map;

import cn.com.xy.sms.sdk.ui.popu.util.SmartSmsSdkUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions.Callback;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.app.AlertDialog.Builder;

public abstract class BaseActivityForWebViewImp implements IActivityForWebView {

    @Override
    abstract public WebView getWebView();

    @Override
    abstract public Activity getActivity();
    
    @Override
    abstract public Intent getIntent();

    @Override
    public int checkOrientation(){
        if(getActivity() != null){
            int currentOrientation = getActivity().getResources().getConfiguration().orientation;
            return currentOrientation;
        }
        return -1;
    };
    
    //request webview to load url,default would call getWebView().toloadurl 
    @Override
   public void requestWebViewLoad(String pageViewUrl,Map<String, String> header){
        if(getWebView() != null){
            getWebView().loadUrl(pageViewUrl,header);
        }
    }
    
    @Override
   public void requestWebViewLoad(String pageViewUrl){
        if(getWebView() != null){
            getWebView().loadUrl(pageViewUrl);
        }
    }

    //errorCode see SdkWebViewManager ERROR_TYPE_OTHER/ERROR_TYPE_NO_NET_WORK
    //extend not used
    @Override
    public void showError(int errorCode, Map<String, Object> extend) {
    }
    
    //other unused
    @Override
    public void setEndUrl(String url) {
    }
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(getActivity() == null){
            return false;
        }

        if (url != null
                && !url.toLowerCase(Locale.getDefault()).startsWith(
                        "http")) {
            try {
                if (url.indexOf("tel:") >= 0) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri
                            .parse(url));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getActivity().startActivity(intent);
                } else {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    getActivity().startActivity(intent);
                }
            } catch (Throwable e) {
                SmartSmsSdkUtil.smartSdkExceptionLog("SdkWebActivity error ", e);
            }
            return true;
        }
        return false;
    }

    //setWebViewClient
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if(view != null){
            view.setVisibility(View.VISIBLE);
        }
    }

    //setWebViewClient
    @Override
    public void onPageFinished(WebView view, String url) {
        if(getWebView() != null){
            getWebView().getSettings().setBlockNetworkImage(false);
        }
    }

    //setWebViewClient
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        if (errorCode != -10) {
            view.stopLoading();
            view.clearView();
            showError(errorCode,null);
        }
    }

    //setWebViewClient
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }
    
    //setDownloadListener
    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
            long contentLength) {
        if(getActivity()!= null){
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            getActivity().startActivity(intent);
        }
    }

  //setWebChromeClient
    @Override
    public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
        if(getActivity() != null){
            Builder builder = new Builder(getActivity());
            builder.setTitle("Alert");
            builder.setMessage(message);
            builder.setPositiveButton(android.R.string.ok,
                    new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            result.confirm();
                        }
                    });
            builder.setCancelable(false);
            builder.create();
            builder.show();
            return true;
        }
        
        return false;
    }

  //setWebChromeClient
    @Override
    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
        if(getActivity() != null){
            Builder builder = new Builder(getActivity());
            builder.setTitle("confirm");
            builder.setMessage(message);
            builder.setPositiveButton(android.R.string.ok,
                    new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            result.confirm();
                        }
                    });
            builder.setNegativeButton(android.R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            result.cancel();
                        }
                    });
            builder.setCancelable(false);
            builder.create();
            builder.show();
            return true;
        }
        return false;
    }

    //setWebChromeClient
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        view.requestFocus();
    }
    
    //setWebChromeClient
    @Override
    public void onConsoleMessage(String message, int lineNumber, String sourceID) {
    }
    
    //setWebChromeClient
    @Override
    public boolean onConsoleMessage(ConsoleMessage cm) {
        return true;
    }

    //setWebChromeClient
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
        callback.invoke(origin, true, false);
    }

    //setWebChromeClient
    @Override
    public void onReceivedTitle(WebView view, String title) {
    }
    
    @Override
    public void onSdkParamsInitfinish(){
    }
}
